package com.huluobo.a2206douyindemo.mine;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.viewpager2.widget.ViewPager2;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.SPUtils;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.huluobo.a2206douyindemo.R;
import com.huluobo.a2206douyindemo.adapter.CommonPagerAdapter;
import com.huluobo.a2206douyindemo.databinding.FragmentMineBinding;
import com.huluobo.a2206douyindemo.login.LoginActivity;
import com.huluobo.a2206douyindemo.setting.SettingActivity;
import com.huluobo.a2206douyindemo.utils.ImageUtils;
import com.huluobo.base.bean.UserBeanResult;
import com.huluobo.base.bean.VideoBeanResult;
import com.huluobo.base.db.AppDBUtils;
import com.huluobo.base.mvvm.BaseMVVMFragment;
import com.huluobo.base.net.Const;

import java.util.ArrayList;
import java.util.List;

public class MineFragment extends BaseMVVMFragment<FragmentMineBinding, MineViewModel> {
    private ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();
    private CommonPagerAdapter commonPagerAdapter;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initData() {
        viewModel.queryCollectVideos();

        viewModel.getCollectVideosSuccess.observe(this, videoBeans -> {
            Log.i("TagA", "videos:" + videoBeans.size());
        });

        viewModel.getCollectVideosFailed.observe(this, s -> {
            Log.i("TagA", "获取收藏视频失败:" + s);
        });

        viewModel.queryUserInfoByName(SPUtils.getInstance().getString(Const.PARAM_USERNAME));
        viewModel.getUserInfoSuccess.observe(this, userBean -> {
            ImageUtils.getInstance().loadImage(requireContext(), userBean.getIcon(), binding.mineUserIconIv);
            binding.mineNicknameTv.setText(userBean.getNickname());
        });

        viewModel.getUserInfoFailed.observe(this, s -> {
            Log.i("TagA", "获取用户信息失败:" + s);
        });


    }

    @Override
    protected void initView() {
        tabEntities.clear();
        tabEntities.add(new TabEntity("点赞"));
        tabEntities.add(new TabEntity("收藏"));
        binding.mineTabLayout.setTabData(tabEntities);

        fragments.clear();
        fragments.add(new LikeFragment());
        fragments.add(new CollectFragment());

        commonPagerAdapter = new CommonPagerAdapter(requireActivity(), fragments);
        binding.mineVp2.setAdapter(commonPagerAdapter);

        //设置tab和vp2的联动
        binding.mineTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                binding.mineVp2.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        //设置vp2和tab联动
        binding.mineVp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                binding.mineTabLayout.setCurrentTab(position);
            }
        });

        if (SPUtils.getInstance().getString(Const.PARAM_TOKEN).isEmpty()) {
            binding.mineNicknameTv.setText("登录/注册");
            binding.mineNicknameTv.setOnClickListener(v -> {
                startActivity(new Intent(requireContext(), LoginActivity.class));
            });
        }

        binding.mineSettingIv.setOnClickListener(v -> {
            startActivity(new Intent(requireContext(), SettingActivity.class));
        });
    }

    @Override
    protected Class<MineViewModel> getViewModel() {
        return MineViewModel.class;
    }
}